<?php
/**
 * 毛文科
 */
namespace tenxun\bind\weixin;
use tenxun\bind\Config;

class XiaoChengXuZhiFu extends Config
{
    protected $mch_id = '';
    protected $mch_key = '';
    protected $notify_url = '';
    /**设置商户id
     * @param 商户id $mch_id
     * @return $this
     */
    public function setMchId($mch_id){
        $this->mch_id = $mch_id;
        return $this;
    }

    /**获取商户id
     * @return string
     */
    public function getMchId(){
        return $this->mch_id;
    }
    /**设置商户key
     * @param 商户key $mch_key
     * @return $this
     */
    public function setMchKey($mch_key){
        $this->mch_key = $mch_key;
        return $this;
    }
    /**获取商户key
     * @return mixed
     */
    public function getMchKey(){
        return $this->mch_key;
    }

    /**设置异步回调
     * @param $notify_url
     * @return $this
     */
    public function setNotifyUrl($notify_url){
        $this->notify_url = $notify_url;
        return $this;
    }

    /**获取异步回调
     * @return string
     */
    public function getNotifyUrl(){
        return $this->notify_url;
    }

    /**支付签名单设置
     * @param 交易金额 $total_fee
     * @param string交易类型 $trade_type
     * @param null异步通知地址 $notify_url
     * @param string调用微信支付API的机器IP $spbill_create_ip
     * @param null商户系统内部订单号 $out_trade_no
     * @param null微信用户在商户对应appid $openid
     * @param null商品ID $product_id
     * @param null商品描述 $body
     * @param null商品详情 $detail
     * @param null附加数据 $attach
     * @param null随机字符串 $nonce_str
     * @param string签名类型 $sign_type
     * @return string
     * @throws Exception
     */
    public function staticPaymentSign($total_fee,$openid,$body='这是商品',$trade_type='JSAPI',
                                      $notify_url=null,$spbill_create_ip='127.0.0.1',
                                      $out_trade_no=null,$product_id=null,
                                      $detail=null,$attach=null,
                                      $nonce_str=null,$sign_type='MD5'){
        $post['appid'] = $this->getAppid();
        $post['mch_id'] = $this->getMchId();
        $post['total_fee'] = (int)($total_fee*100);
        if($trade_type){
            $post['trade_type'] = $trade_type;
        }
        if($notify_url){
            $post['notify_url'] = $notify_url;
        }else{
            $post['notify_url'] = $this->getNotifyUrl();
        }
        if($spbill_create_ip){
            $post['spbill_create_ip'] = $spbill_create_ip;
        }
        if($out_trade_no){
            $post['out_trade_no'] = $out_trade_no;
        }else{
            $post['out_trade_no'] = uniqid();
        }
        if($openid){
            $post['openid'] = $openid;
        }else{
            throw new Exception('请设置openid');
        }
        if($product_id){
            $post['product_id'] = $product_id;
        }
        if($body){
            $post['body'] = $body;
        }
        if($detail){
            $post['detail'] = $detail;
        }
        if($attach){
            $post['attach'] = $attach;
        }
        if($nonce_str){
            $post['nonce_str'] = $nonce_str;
        }else{
            $post['nonce_str'] = uniqid();
        }

        if($sign_type){
            $post['sign_type'] = $sign_type;
        }
        $post['sign'] = $this->getSign($post,$sign_type);
        $post_xml = "<xml>";
        foreach ($post as $key=>$value){
            $post_xml .="<$key>$value</$key>";
        }
        $post_xml.="</xml>";
        $curl = new Curl();
        $array = $curl->fasong("https://api.mch.weixin.qq.com/pay/unifiedorder",1,1,$post_xml)->xml()->get();
        if(isset($array['return_msg'])&&($array['return_msg']=='ok'||$array['return_msg']=='OK')){
            $time = (string)time();
            $newsign['appId'] = $this->getAppId();
            $newsign['nonceStr'] = $post['nonce_str'];
            $newsign['package'] = 'prepay_id='.$array['prepay_id'];
            $newsign['signType'] =  $post['sign_type'];
            $newsign['timeStamp'] =$time;
            $newsign['paySign'] = $this->getSign($newsign);
            $newsign['out_trade_no'] = $post['out_trade_no'];
        }else{
            return $array;
        }
        return $newsign;
    }

    /**获取签名
     * @param array $data
     * @param $type
     * @return string
     */
    public function getSign($data=[],$type){
        if(!$data){
            throw new Exception('签名没有数据');
        }
        ksort($data);
        if($this->getMchKey()){
            $data['key'] = $this->getMchKey();
        }
        $str = '';
        foreach ($data as $key=>$value){
            if(!$value){
                continue;
            }else{
                if(!$str){
                    $str = (string)$key."=$value";
                }else{
                    $str .= "&".$key."=$value";
                }
            }
        }
        $res = '';
        if($type=='md5'||$type=='MD5'){
            $res = strtoupper(md5($str));
        }elseif($this->getSignType()=='HMAC-SHA256'){
            $res = strtoupper(hash_hmac('sha256',$str));
        }
        return $res;
    }
    /**回调函数
     * @param Request $request
     * @param $func
     */
    public function HuiDiao(Request $request){
        $xml = file_get_contents("php://input");
        $xml = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
        $post_data  = (array)$xml;
        return ['data'=>$post_data,'str'=>xml([
            'return_code'=>'SUCCESS',
            'return_msg'=>'OK'
        ])];
    }
}